<!--
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-08-14 10:00:17
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2025-01-13 17:10:40
 * @FilePath: \BLOG\src\views\play\game\index.vue
-->
<template>
  <div class="game-out-warp">
    <PageTopCover
      :moduleTitle="'小游戏'"
      :icon="'icon-yuleAPP'"
      :coverImg="getOnlineImgUrl('/imgs/bg/73.png')"
      :isMobile="isMobi"
      :mudulDesc="'这里有很多好玩的小游戏，快来玩！'"
      :isArticle="false"></PageTopCover>
    <div class="game-container" :style="{ padding: state.page.total <= state.page.size ? '20px 0' : '20px 0 0  0' }" :class="{ 'm-game-container': isMobi }">
      <div class="center-max-width-warp">
        <div class="game-content-warp">
          <div class="game-warp-list" v-if="state.list.length > 0">
            <div class="game-item pointer intersection-observer kbn-custom hover-shadow" :data-tip="'点击开局 ' + item.title" @click="handleGame(item)" v-for="item in state.list">
              <div class="game-item-img">
                <LImg class="game-img" :src="item.cover" :isUnPreview="true" alt="" />
              </div>
              <div class="game-item-title">{{ item.title }}</div>
            </div>
          </div>
          <div class="no-article" v-else>
            <Empty :text="'暂无资源，期待您的分享~'" :loadingText="'资源正在拼命加载中...'" :loading="state.loading" />
          </div>

          <LoadingMore
            v-if="store.projectConfig.pageType == 'more'"
            :loading="state.loading"
            :hasMore="state.isMore"
            :length="state.list.length"
            @loadMore="handleLoadMore"></LoadingMore>
          <CustomPage v-else :page="state.page" @change="handlePageChange" :loading="state.loading" scorllTop="game-container"></CustomPage>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getArticleList } from '@/api/article';
import useResize from '@/hooks/useResize';
import { getOnlineImgUrl } from '@/utils/common';
import useIntersectionObserver from '@/hooks/useIntersectionObserver';
import { useAppStore } from '@/store/app';
import { reactive } from 'vue';

const store = useAppStore();
const { isMobi } = useResize();
useIntersectionObserver();

const state = reactive({
  list: [],
  loading: false,
  isMore: true,
  page: {
    page: 1,
    size: 9,
    total: 0
  }
});

// 获取小游戏列表
const getListFn = scorllTopCallback => {
  state.loading = true;
  getArticleList({ ...state.page, game: 1 })
    .then(res => {
      if (res.code === 200) {
        res.data.forEach(item => {
          (item.title = item.title.includes('（') ? item.title.split('（')[0] : item.title),
            (item.cover = import.meta.env.VITE_CURRENT_ENV == 'dev' ? import.meta.env.VITE_DEV_BASE_SERVER + item.cover : store.projectConfig.bsServerUrl + item.cover);
        });

        if (store.projectConfig.pageType == 'more') {
          state.list = [...state.list, ...res.data];
        } else {
          state.list = res.data;
        }
        state.page.total = res.total;
        if (state.list.length >= res.total) {
          state.isMore = false; // 已经没有更多数据了
        }
        scorllTopCallback && scorllTopCallback();
        state.loading = false;
      }
    })
    .finally(() => {
      state.loading = false;
    });
};
getListFn(false);

// 加在更多
const handleLoadMore = () => {
  state.loading = true;
  state.page.page++;
  getListFn(false);
};

// 分页改变
const handlePageChange = (page: number, size: number, scorllTopCallback: Function) => {
  state.page.page = page;
  state.page.size = size;
  getListFn(scorllTopCallback);
};

// 点击小游戏跳转到预览页
const handleGame = (row: any) => {
  let urlPrefix = store.projectConfig.blogUrl;
  window.open(urlPrefix + '/preview?id=' + row.id, '_blank');
};
</script>

<style lang="scss" scoped>
.game-out-warp {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  .game-container {
    width: 100%;
    height: 100%;
    min-height: calc(100vh - 400px);
    background-color: var(--bg-image-warp-color);
    .center-max-width-warp {
      height: 100%;
      max-width: var(--content-max-width);
      margin: 0 auto;
      .game-content-warp {
        // background-color: var(--bg-content-color);
        border-radius: var(--border-radius-2);
        .game-warp-list {
          display: flex;
          flex-wrap: wrap;
          .game-item {
            border-radius: var(--border-radius-2);
            width: 280px;
            height: 200px;
            background-color: var(--bg-content-color);
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
            margin: 10px 13px;
            display: flex;
            flex-direction: column;
            align-items: center;
            position: relative;
            overflow: hidden;
            &:hover {
              .game-img {
                transform: scale(1.1);
              }
            }
            .game-item-img {
              width: 220px;
              height: 220px;
              width: 100%;
              height: 100%;
              overflow: hidden;
              transition: all 0.6s ease-in-out;
              .game-img {
                width: 100%;
                height: 100%;
                object-fit: cover;
                border-radius: 5px;
                transition: all 0.6s ease-in-out;
              }
            }
            .game-item-title {
              width: 100%;
              font-size: 18px;
              text-align: center;
              color: var(--text-color);
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 1; /* 定义文本的行数 */
              overflow: hidden;
              text-overflow: ellipsis;
              position: absolute;
              bottom: 0px;
              left: 0;
              height: 30px;
              line-height: 30px;
              background-color: var(--bg-content-color);
            }
          }
        }
      }
    }
  }
  .m-game-container {
    min-height: calc(100vh - 300px);
    .center-max-width-warp {
      width: var(--content-max-width-m) !important;
      .game-item {
        width: 100% !important;
        height: 48vw !important;
        margin: 10px 0 !important;
        .game-item-title {
          bottom: 0 !important;
          font-size: 20px !important;
        }
      }
    }
  }
}
</style>
